package butines.game.lastcolony;

import java.util.List;

import butines.core.GameImage;
import butines.game.lastcolony.entities.Entity;
import butines.game.lastcolony.entities.EntityTeam;
import butines.game.lastcolony.entities.Game;
import butines.game.lastcolony.entities.GameCommand;
import butines.game.lastcolony.entities.Level;
import butines.game.lastcolony.entities.Maps;
import butines.game.lastcolony.screen.GameInterfaceScreen;
import butines.game.lastcolony.screen.GameStartScreen;
import butines.game.lastcolony.screen.MissionScreen;

public class SinglePlayer {

    private LastColony lastColony;
    
    
	private int currentLevel;
	
	public SinglePlayer(LastColony lastColony) {
	    this.lastColony = lastColony;
	}
	
	public void start() {
	    getGame().setType("singleplayer");
	    getGame().setTeam(EntityTeam.BLUE);
		currentLevel = 0;
		startCurrentLevel();
	}
	
	private Game getGame() {
	    return lastColony.getGame();
	}
	
	private Maps getMaps() {
        return lastColony.getMaps();
    }

    public GameStartScreen getGameStartScreen() {
        return lastColony.getGameStartScreen();
    }

    public MissionScreen getMissionScreen() {
        return lastColony.getMissionScreen();
    }

    public GameInterfaceScreen getGameInterfaceScreen() {
        return lastColony.getGameInterfaceScreen();
    }
	
	public void startCurrentLevel() {
		Level level = getMaps().getSinglePlayer().get(currentLevel);
		
		GameImage currentMapImage = new GameImage(level.getMapImage());
		
		getGame().setCurrentMapImage(currentMapImage);
		getGame().setCurrentLevel(level);
		
        getGame().setOffsetX(level.getStartX() * getGame().getGridSize());
        getGame().setOffsetY(level.getStartY() * getGame().getGridSize());
		
        for (Entity entity : level.getItems()) {
            getGame().add(entity);
        }
        
        // Create a grid that stores all obstructed tiles as 1 and unobstructed as 0
        int[][] currentMapTerrainGrid = new int[level.getMapGridHeight()][];
        int[][] currentMapPassableGrid = new int[level.getMapGridHeight()][];
        for (int y = 0; y < level.getMapGridHeight(); y++) {
            currentMapTerrainGrid[y] = new int[level.getMapGridWidth()];
            currentMapPassableGrid[y] = new int[level.getMapGridWidth()];
        }
        
        for (int i = level.getMapObstructedTerrain().length - 1; i >= 0; i--) {
            int[] obstruction = level.getMapObstructedTerrain()[i];
            int x = obstruction[0];
            int y = obstruction[1];
            currentMapTerrainGrid[y][x] = 1;
        }
        
        getGame().setCurrentMapTerrainGrid(currentMapTerrainGrid);
        getGame().setCurrentMapPassableGrid(currentMapPassableGrid);
        
        getGame().rebuildPassableGrid();
        
		getMissionScreen().setBriefing(level.getBriefing());
		getMissionScreen().show();
	}

	public void play() {
	    getGame().start();
	}
	
    public void exit() {
        getGameStartScreen().show();
    }

    public void sendCommand(List<Integer> uids, GameCommand cmd) {
        getGame().processCommand(uids, cmd);
    }
	
}
